package fu.capstone.hanoicityguide.presentation.fragment.attraction;

import org.mapsforge.core.model.LatLong;
import org.mapsforge.map.android.view.MapView;
import org.mapsforge.map.layer.Layers;
import org.mapsforge.map.layer.cache.TileCache;
import org.mapsforge.map.layer.overlay.Marker;
import org.mapsforge.map.layer.renderer.TileRendererLayer;

import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import fu.capstone.hanoicityguide.R;
import fu.capstone.hanoicityguide.business.bo.attraction.AttractionBO;
import fu.capstone.hanoicityguide.business.bo.attraction.AttractionBOImp;
import fu.capstone.hanoicityguide.business.model.Attraction;
import fu.capstone.hanoicityguide.common.offlinemapultility.OfflineMapUtil;
import fu.capstone.hanoicityguide.common.utility.LocationSetting;

public class AttractionOfflineMapFragment extends Fragment {

	private OfflineMapUtil util;
	private MapView mapview;
	private TileCache tileCache;
	private TileRendererLayer tileRendererLayer;
	private AttractionBO attractionBO;
	private LatLong attraction_geo;
	private LocationSetting locationSetting;
	private Attraction attraction;

	public AttractionOfflineMapFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View attractionOfflineMapFragment = inflater.inflate(
				R.layout.offline_map_fragment, container, false);

		try {
			// AndroidGraphicFactory.createInstance(getActivity().getApplication());
			// mapview = new MapView(container.getContext());
			// setContentView(mapview);
			// mapview = (MapView)
			// attractionOfflineMapFragment.findViewById(R.id.mapView);
			util = new OfflineMapUtil(container.getContext());
			mapview = (MapView) attractionOfflineMapFragment
					.findViewById(R.id.mapView);

			/* Get place data */
			Bundle bundle = this.getArguments();
			int attractionID = bundle.getInt("AttractionID", 0);
			if (attractionID != 0) {

				attractionBO = new AttractionBOImp(container.getContext());
				attraction = attractionBO.browseAttraction(attractionID);
				attraction_geo = util.convertGeoCoordinateToLatLong(attraction
						.getGeo());
			}
			/* Setup map and create markers */
			locationSetting = new LocationSetting(container.getContext());
			if (locationSetting.getGPSSetting(container.getContext())) {
				util.setupofflinemap(mapview, container.getContext());
				Marker placeMarker = util.createMarker(container.getContext(),
						attraction_geo, attraction.getName(),
						attraction.getType());
				LatLong mylocation = util.getcurrentLocation(
						container.getContext(), mapview);

				Marker mylocationMarker = util.createMyLocationMarker(
						container.getContext(), mylocation,
						R.drawable.mylocationmarker);

				Layers layers = this.mapview.getLayerManager().getLayers();
				layers.add(placeMarker);
				layers.add(mylocationMarker);

				/* routing */
				util.initHopper();
				util.calcPath(mylocation.latitude, mylocation.longitude,
						attraction_geo.latitude, attraction_geo.longitude,
						mapview);
			} else {
				locationSetting.showSettingsAlert();
			}
		} catch (Exception e) {
			Log.v("Attraction offline map", e.toString());
		}
		
		return attractionOfflineMapFragment;
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

}
